package com.example.f8399.testdemo.adapter;

import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.bean.ExpandLev1;
import com.example.f8399.testdemo.bean.ExpandLev2;

import java.util.List;

/**
 * Created by lcfeng on 2018/11/24/024
 * Describe:
 */
public class ExpandableListadapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {


    public static final int TYPE_1 = 1;
    public static final int TYPE_2 = 2;


    public ExpandableListadapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_1, R.layout.item_expand_lev1);
        addItemType(TYPE_2, R.layout.item_expand_lev2);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_1:
                final ExpandLev1 lv1 = (ExpandLev1) item;
                helper.setText(R.id.tv_title, lv1.getTitle())
                .setImageResource(R.id.iv_action,lv1.isExpanded()?R.drawable.ic_expand:R.drawable.ic_collect);

                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int layoutPosition = helper.getLayoutPosition();
                        if (lv1.isExpanded()) {
                            collapse(layoutPosition);
                        }else {
                            expand(layoutPosition);
                        }
                    }
                });
                break;
            case TYPE_2:
                ExpandLev2 lv2 = (ExpandLev2) item;
                helper.setText(R.id.tv_msg, ((ExpandLev2) item).getMsg());
                break;
            default:
                LogUtils.e("未找到类型适配");
                break;
        }
    }
}
